//
//  TouchOneByOneScene.cpp
//  SimpleGame
//
//  Created by Scar1et on 2017/8/1.
//
//

// 头文件 -- 类库声明请放入头文件
#include "TouchOneByOneScene.h"

// 层级：影响显示与事件响应
enum {
    // 背景
    levelBGSimple = 1,
    // 内容
    levelContentSimple = 101,
    // 对话框
    levelDialogSimple = 1001
};

// TAG：视图标记
enum {
    tagBack = 1,
    tagIconSpirit = 2
};

// 创建场景
Scene* TouchOneByOneScene::createScene() {
    Scene* sTouchOneByOne = Scene::create();
    sTouchOneByOne->addChild(TouchOneByOneScene::create());
    return sTouchOneByOne;
}

// 初始化场景，自动调用
bool TouchOneByOneScene::init() {
    // 先调用父类Init
    if (!Layer::init()) {
        return false;
    }
    
    // 手机按键监听器
    listenerKeyboard = EventListenerKeyboard::create();
    listenerKeyboard->onKeyReleased = CC_CALLBACK_2(TouchOneByOneScene::onKeyReleased, this);
    // 添加到事件分发器中
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerKeyboard, this);
    
    // 单点触控监听器
    listenerTouchOneByOne = EventListenerTouchOneByOne::create();
    // 是否吞噬【不向下传递】事件
    listenerTouchOneByOne->setSwallowTouches(true);
    // 开始
    // 当返回true时 ，表示继续响应TouchMoved、TouchEnded事件
    // 当返回false时，则不再继续响应这两个事件
    listenerTouchOneByOne->onTouchBegan = CC_CALLBACK_2(TouchOneByOneScene::onTouchBegan, this);
    // 移动
    listenerTouchOneByOne->onTouchMoved = CC_CALLBACK_2(TouchOneByOneScene::onTouchMoved, this);
    // 取消，一般是系统层级的消息，如来电话，触摸事件就会被打断
    listenerTouchOneByOne->onTouchCancelled = CC_CALLBACK_2(TouchOneByOneScene::onTouchCancelled, this);
    // 结束
    listenerTouchOneByOne->onTouchEnded = CC_CALLBACK_2(TouchOneByOneScene::onTouchEnded, this);
    // 添加到事件分发器中
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerTouchOneByOne, this);
    
    // 开始初始化
    // 成员变量初始化
    // 可视区域尺寸
    sizeVisible = Director::getInstance()->getVisibleSize();
    // 可视区域的出发点坐标
    positionOrigin = Director::getInstance()->getVisibleOrigin();
    // 屏幕尺寸
    sizeWin = Director::getInstance()->getWinSize();
    
    this->initContent();
    
    // 初始化成功
    return true;
}

// 内容初始化
void TouchOneByOneScene::initContent() {
    // 返回按钮
    auto lBack = Label::createWithSystemFont("返回", "arial.ttf", 28);
    lBack->setTextColor(Color4B::WHITE);
    lBack->setHorizontalAlignment(TextHAlignment::LEFT);
    lBack->setVerticalAlignment(TextVAlignment::CENTER);
    lBack->setDimensions(120, 40);
    auto milBack = MenuItemLabel::create(lBack, CC_CALLBACK_1(TouchOneByOneScene::onClicked, this));
    milBack->setTag(tagBack);
    milBack->setPosition(Vec2(positionOrigin.x + 80, positionOrigin.y + sizeVisible.height - 40));
    
    // 按钮菜单 -- TouchOneByOne【可添加多个，用NULL结尾】
    auto mTouchOneByOne = Menu::create(milBack, NULL);
    mTouchOneByOne->setPosition(Vec2::ZERO);
    this->addChild(mTouchOneByOne, levelContentSimple);
    
    // 精灵图标
    auto sIconSpirit = Sprite::create("icon_spirit.png");
    sIconSpirit->setPosition(Vec2(positionOrigin.x + sizeVisible.width / 2, positionOrigin.y + sizeVisible.height / 2));
    this->addChild(sIconSpirit, levelContentSimple, tagIconSpirit);
}

// 返回功能
void TouchOneByOneScene::back() {
    // 从事件分发器移除单点触控监听器
    Director::getInstance()->getEventDispatcher()->removeEventListener(listenerTouchOneByOne);
    // 从事件分发器移除手机按键监听器
    Director::getInstance()->getEventDispatcher()->removeEventListener(listenerKeyboard);
    // 从场景堆栈中pop出一个场景，替换现在正运行的场景，而运行中的场景将被删除
    Director::getInstance()->popScene();
}

// 点击监听
void TouchOneByOneScene::onClicked(Ref* rSender) {
    int tag = ((MenuItem*)rSender)->getTag();
    switch (tag) {
        case tagBack:
            // 返回按钮被点击
            this->back();
            break;
        default:
            break;
    }
}

// 键盘点击监听
void TouchOneByOneScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) {
    switch (keyCode){
        case EventKeyboard::KeyCode::KEY_BACK:
            // 返回键
            this->back();
            break;
        default:
            break;
    }
}

// 触控
// 开始
bool TouchOneByOneScene::onTouchBegan(Touch* touch, Event* event) {
    // 精灵
    auto sIconSpirit = this->getChildByTag(tagIconSpirit);
    // 移动到手触碰的位置
    sIconSpirit->setPosition(touch->getLocation());
    // 继续响应TouchMoved、TouchEnded事件
    return true;
}

// 移动
void TouchOneByOneScene::onTouchMoved(Touch* touch, Event* event) {
    // 精灵
    auto sIconSpirit = this->getChildByTag(tagIconSpirit);
    // 移动到移动了偏移量之后的位置
    sIconSpirit->setPosition(sIconSpirit->getPosition() + touch->getDelta());
}

// 取消
void TouchOneByOneScene::onTouchCancelled(Touch* touch, Event* event) {
    
}

// 结束
void TouchOneByOneScene::onTouchEnded(Touch* touch, Event* event) {
    // 精灵
    auto sIconSpirit = this->getChildByTag(tagIconSpirit);
    // 回到一开始的位置
    sIconSpirit->setPosition(touch->getStartLocation());
}
